package com.bp.project.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.bp.framework.util.SecurityUtils;
import com.bp.project.domain.BizProject;
import com.bp.project.service.IBizProjectService;
import com.bp.system.domain.SysRegion;
import com.bp.system.service.ISysRegionService;
import com.bp.system.service.ISysUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.bp.common.annotation.Log;
import com.bp.common.core.controller.BaseController;
import com.bp.common.core.domain.AjaxResult;
import com.bp.common.enums.EnumBusinessType;
import com.bp.project.domain.BizProjectFinance;
import com.bp.project.service.IBizProjectFinanceService;
import com.bp.common.utils.poi.ExcelUtil;
import com.bp.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 工程财务记录Controller
 * 
 * @author shaominjie
 * @date 2021-01-21
 */
@RestController
@RequestMapping("/project/finance")
public class BizProjectFinanceController extends BaseController {
    @Resource
    private IBizProjectFinanceService bizProjectFinanceService;
    @Resource
    private ISysRegionService regionService;
    @Resource
    private ISysUserService userService;
    @Resource
    private IBizProjectService projectService;
    /**
     * 查询工程财务记录列表
     */
    @PreAuthorize("@ss.hasPermi('project:finance:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizProjectFinance bizProjectFinance) {
        startPage();
        List<SysRegion> regions = regionService.selectList(new SysRegion());
        List<BizProject> projects = projectService.selectList(new BizProject());
        Map<Long,SysRegion> regionMap = new HashMap<>();
        Map<Long,BizProject> projectMap = projects.stream().collect(Collectors.toMap(BizProject::getProjId,e->e));
        if(CollectionUtils.isNotEmpty(regions)){
            regionMap = regions.stream().collect(Collectors.toMap(SysRegion::getRegionId,e->e));
        }
        Map<String,String> userMap = userService.userName2NickName();

        List<BizProjectFinance> list = bizProjectFinanceService.selectList(bizProjectFinance);
        if(CollectionUtils.isNotEmpty(list)){
            for(BizProjectFinance item : list){
                item.setRegion(regionMap.get(item.getRegionId()));
                item.setCreateBy(userMap.get(item.getCreateBy()));
                item.setUpdateBy(userMap.get(item.getUpdateBy()));
                item.setProject(projectMap.get(item.getProjId()));
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出工程财务记录列表
     */
    @PreAuthorize("@ss.hasPermi('project:finance:export')")
    @Log(title = "工程财务记录", businessType = EnumBusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizProjectFinance bizProjectFinance) {
        List<BizProjectFinance> list = bizProjectFinanceService.selectList(bizProjectFinance);
        ExcelUtil<BizProjectFinance> util = new ExcelUtil<BizProjectFinance>(BizProjectFinance.class);
        return util.exportExcel(list, "finance");
    }

    /**
     * 获取工程财务记录详细信息
     */
//    @PreAuthorize("@ss.hasPermi('project:finance:query')")
    @GetMapping(value = "/{financeId}")
    public AjaxResult getInfo(@PathVariable("financeId") Long financeId) {
        return AjaxResult.success(bizProjectFinanceService.selectById(financeId));
    }

    /**
     * 新增工程财务记录
     */
//    @PreAuthorize("@ss.hasPermi('project:finance:add')")
    @Log(title = "工程财务记录", businessType = EnumBusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProjectFinance bizProjectFinance) {
        bizProjectFinance.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bizProjectFinanceService.insert(bizProjectFinance));
    }

    /**
     * 修改工程财务记录
     */
//    @PreAuthorize("@ss.hasPermi('project:finance:edit')")
    @Log(title = "工程财务记录", businessType = EnumBusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProjectFinance bizProjectFinance) {
        bizProjectFinance.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(bizProjectFinanceService.update(bizProjectFinance));
    }

    /**
     * 删除工程财务记录
     */
    @PreAuthorize("@ss.hasPermi('project:finance:remove')")
    @Log(title = "工程财务记录", businessType = EnumBusinessType.DELETE)
	@DeleteMapping("/{financeIds}")
    public AjaxResult remove(@PathVariable Long[] financeIds) {
        return toAjax(bizProjectFinanceService.deleteByIds(financeIds));
    }
}
